package com.xzy.learn.spring.bean.definition;

import com.xzy.learn.spring.entity.User;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 注册外部对象
 *
 * @author xzy.xiao
 * @date 2022/6/6  9:44
 */
public class ExternalBeanRegistrationDemo {
    public static void main(String[] args) {
        registrationExternalBean();
    }

    public static void registrationExternalBean() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();

        // 注册外部单例对象
        User externalBeanSet = new User(1001L, "Jack");
        beanFactory.registerSingleton("external_user", externalBeanSet);
        applicationContext.refresh(); // 注意：

        User externalBeanGet = applicationContext.getBean("external_user", User.class);
        System.out.println(externalBeanGet);
        applicationContext.close();
    }
}
